import getApiData, { getNestApiData, onlineGetApiData } from "@/services/cpasRequest";
import { message } from "antd";
import { request } from "umi";

/**
 * 获取组成信息列表
 * @param params 
 * @returns 
 */
export async function getInformationList(params: {
  zcbfid: string;
}) {
  const res = await getApiData('home.zcbfInfo', params);
  if (res.ok) {
    return res.data;
  };
  message.error(res.data);
  return [];
}

// 获取期间列表
export async function getDateList(params: {
  zcbfid: string;
}) {
  const res = await getApiData('home.zcbfAuditSection', params);
  if (res.ok) {
    return res.data;
  };
  message.error(res.data);
  return [];
}

// 更新 列表 SJXM_ZCBF
export async function getZCBFDateUpdate(params: {
  bsdw: string,
  shortName: string,
  sjnd: string,
  ReportType: string,
  AuditReportTemplate: number,
  ReceivePerson: string,
  startTime: string,
  zcbfid: any
}) {
  // 插入的模板
  // let AuditReportTemplateNumber=Number(params.AuditReportTemplate) 
  const data = [];
  data.push({
    rowState: "update",
    bsdw: params.bsdw,
    ShortName: params.shortName,
    sjnd: params.sjnd,
    ReportType: params.ReportType,
    AuditReportTemplate: params.AuditReportTemplate,
    ReceivePerson: params.ReceivePerson,
    AuditReportDate: params.startTime,
    where: {
      zcbfID: params.zcbfid,
    }
  })
  const r = await getApiData("sqlApi.update", {
    name: "SJXM_ZCBF",
    rows: data
  });
  if (r.ok) return r.ok;
}

// 更新 列表 CR_SenderInfo
export async function getInfoDateUpdate(params: {
  Address: string,
  Fax: string,
  Phone: string,
  SWSEMC: string,
  SWSMC: string,
  website: string,
  id: string
}) {
  // 插入的模板
  const data = [];
  data.push({
    rowState: "update",
    Address: params.Address,
    Fax: params.Fax,
    Phone: params.Phone,
    SWSEMC: params.SWSEMC,
    SWSMC: params.SWSMC,
    website: params.website,
    where: {
      ID: params.id,
    }
  })
  const r = await getApiData("sqlApi.update", {
    name: "CR_SenderInfo",
    rows: data
  });
  if (r.ok) return r.ok;
}


// 更新 云平台
export async function zcbfInfoSave(params: {
  dbName: string,
  projectCode: string,
  zcbfID: string,
  receivePerson: string,
  auditReportDate: string,
  auditReportTemplate: string,
  reportType: string,
  periods: any[]
}) {
  const res = await onlineGetApiData('cpas6.pm.zcbfInfoSave', params);
  if (res.ok) {
    return res.data;
  };
  return [];
}

/**
 * 获取集团模板
 */
export async function getCloudGroupTemplateList(
  params: {
    dbName: string,
    projectCode: string,
    status?: number,
    searchText?: string,
  }
) {
  const retData = await onlineGetApiData('groupTemplate.groupTemplate.list', {
    dbName: params.dbName,
    projectCode: params.projectCode,
    status: params.status,
    searchText: params.searchText,
  });
  if (retData.ok) {
    return Promise.resolve({
      data: retData.data.rows,
      success: true,
    });
  }
  return Promise.resolve({
    data: [],
    success: false,
  });

}

// 获取报告模板
export async function templateListApi() {
  try {
    const res = await getNestApiData('/template/list', 'get', {})
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口/template/list,请求失败！',
      success: false,
    });
  }
}

// 同步
export async function getPeriodInfo(params: {
  dbname: string;
  bgType: string;
}) {
  try {
    const res = await getApiData("account.getPeriodInfo", params);
    if (res.ok) {

      const settingCol = {
        dbname: params.dbname,
        yearCnt: res.data.yearCnt,
        minData_id: res.data.minData_id,
        bgType: params.bgType
      }
      const resCol = await getApiData("account.calcReportSettingColumn", settingCol);
      if (resCol.ok) {
        return Promise.resolve({
          data: res.data,
          success: true,
        });
      }
      return Promise.resolve({
        data: res.data,
        success: false,
      });
    }
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  }
}

